package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.log.InitLog;
import com.example.log.UpdateLog;
import com.example.pojo.OriginPlace;
import com.example.pojo.OriginPlace;
import com.example.mapper.OriginPlaceMapper;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.IOriginPlaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vd.vo.OriginPlaceVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzy'
 * @since 2024-08-11
 */
@Service
@Slf4j

public class OriginPlaceServiceImpl extends ServiceImpl<OriginPlaceMapper, OriginPlace> implements IOriginPlaceService {
    public Result getPlaceList(PageBean pageBean) {
        Long currentPage = pageBean.getCurrentPage();
        Long pageSize = pageBean.getPageSize();
        LambdaQueryWrapper<OriginPlace> wrapper=new LambdaQueryWrapper<OriginPlace>()
                .eq(OriginPlace::getIsDeleted,0);
        Page<OriginPlace> page= new Page<>(currentPage, pageSize);
        page(page,wrapper);
        List<OriginPlace> list = page.getRecords();
        return Result.succeed(list);
    }
    public Result deletedById(Long id) {
        /*boolean type = removeById(id);*/
        LambdaUpdateWrapper<OriginPlace> wrapper=new LambdaUpdateWrapper<OriginPlace>()
                .eq(OriginPlace::getId,id)
                .set(OriginPlace::getIsDeleted,1);
        boolean type = update(wrapper);
        return type ? Result.succeed() : Result.error();
    }
    @UpdateLog
    public Result updateByPlace(OriginPlaceVo OriginPlaceVo) {
        OriginPlace originPlace = BeanUtil.toBean(OriginPlaceVo, OriginPlace.class);
        LambdaUpdateWrapper<OriginPlace> updateWrapper = new LambdaUpdateWrapper<OriginPlace>()
                .eq(OriginPlace::getIsDeleted, 0)
                .eq(OriginPlace::getId, originPlace.getId());
        log.info("OriginPlace: {}",originPlace);
        boolean type = update(originPlace, updateWrapper);
        return type ? Result.succeed() : Result.error();
    }
    @InitLog
    public Result insertByPlace(OriginPlaceVo OriginPlaceVo) {
        try {
            OriginPlace originPlace = BeanUtil.toBean(OriginPlaceVo, OriginPlace.class);
            originPlace.setId(null);
            System.out.println("OriginPlace: "+originPlace);
            save(originPlace);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return Result.error("必要数据不完整,请检查后重试~");
        }
        return Result.succeed();
    }
}
